
<template>
    <Myhead title="修改密码"></Myhead>
    <div class="form">
        <div class="welcom">修改重置密码</div>
        <div class="box">
            <van-form @failed="onFailed">
                <van-cell-group inset>
                    <van-field v-model="pwd" type="password" name="密码" label="密码" placeholder="密码" />
                    <van-field v-model="newpassword" type="password" name="密码" label="新密码" placeholder="新密码"
                        :rules="[{ pattern, required: true, message: '请填写6-12位字母及数字组成的密码' }]" />
                    <van-field v-model="checkpassword" type="password" name="密码" label="确认密码" placeholder="确认密码"
                        :rules="[{ pattern, required: true, message: '请填写6-12位字母及数字组成的密码' }]" />
                </van-cell-group>
                <div class="btns">
                    <van-button @click="submit" type="primary" native-type="submit">
                        提交
                    </van-button>
                    <van-button @click="clean" type="danger">
                        重置
                    </van-button>
                </div>
            </van-form>
        </div>
    </div>
</template>

<script setup>
import { showToast } from 'vant';
import { onMounted, ref } from 'vue';
import { GetUserInfo, ChangeUseHead } from './userapi';
import { useComon } from '../../Hooks/comon';

const { gotowhere } = useComon()
const pwd = ref('')
const newpassword = ref('')
const checkpassword = ref('')
const pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;

const getlogin = async () => {
    const res = await GetUserInfo()
    console.log(res);
    if (res.data.code == 200) {
        pwd.value = res.data.result.password
        console.log(pwd.value);
    }
}

const clean = () => {
    newpassword.value = ''
    checkpassword.value = ''
}

const changeinfo = async (params) => {
    const res = await ChangeUseHead({ password: params })
    console.log(res);
    if (res.data.code == 200) {
        showToast({
            message: '修改密码成功'
        })
        gotowhere('/user')
    } else {
        showToast({
            message: res.data.msg
        })
    }
}

const submit = () => {
    if (!newpassword.value || !checkpassword) {
        showToast({
            message: '请输入或确认密码'
        })
    } else {
        if (newpassword.value != checkpassword.value) {
            showToast({
                message: '两次密码不同,请重新输入'
            })
            return
        } else {
            if (pattern.test(newpassword.value) && pattern.test(checkpassword.value)) {
                changeinfo(newpassword.value)
            }else{
                console.log(pattern.test(newpassword));
                console.log(pattern.test(checkpassword));
                showToast({
                    message:'密码为6-12位字母和数组组成'
                })
            }
        }
    }
}

onMounted(() => {
    getlogin()

})
</script>


<style lang="scss">
.form {


    .welcom {
        font-size: 16px;
        color: #99A6C4;
        margin-left: 30px;
        line-height: 50px;
        width: 60%;
    }
}

.btns {
    width: 60%;
    display: flex;
    justify-content: space-between;
    margin: 0 auto;
    margin-top: 40px;
}
</style>